/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utils;
import javax.swing.*;
import javax.swing.text.*;
/**
 *
 * @author borysfan
 */
public class TextFieldFilter extends PlainDocument {

   public static final String SPEC =
           "`~!@#$%^&*()_+-={}[]\\|;:'\"<>,./?";
   public static final String LOWERCASE  =
        "abcdefghijklmnopqrstuvwxyz";
   public static final String UPPERCASE  =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   public static final String ALPHA   =
        LOWERCASE + UPPERCASE;
   public static final String NUMERIC =
        "0123456789";
   public static final String FLOAT =
        NUMERIC + ".";
   public static final String ALPHA_NUMERIC =
        ALPHA + NUMERIC;

   public static final String UPPERCASE_NUMERIC =
        UPPERCASE + NUMERIC;
   public static final String SPEC_UP_ALP =
           SPEC + NUMERIC + UPPERCASE;

   private int limit = Integer.MAX_VALUE;
   private boolean upperCase = true;

   protected String acceptedChars = null;
   protected boolean negativeAccepted = false;
   protected boolean onlyDayValues = false;

   public TextFieldFilter() {
     this(ALPHA_NUMERIC);
     }
   public TextFieldFilter(String acceptedchars) {
       
     acceptedChars = acceptedchars;
     }
   public TextFieldFilter(String acceptedchars,int lim) {
     acceptedChars = acceptedchars;
     limit = lim;
   }

   public TextFieldFilter(String acceptedchars,int lim,boolean upper) {
     acceptedChars = acceptedchars;
     limit = lim;
     upperCase=upper;
   }

   public TextFieldFilter(String acceptedchars,int lim,boolean upper,boolean dayValues) {
     acceptedChars = acceptedchars;
     limit = lim;
     upperCase=upper;
     onlyDayValues = dayValues;
   }


   public void setNegativeAccepted(boolean negativeaccepted) {
     if (acceptedChars.equals(NUMERIC) ||
         acceptedChars.equals(FLOAT) ||
         acceptedChars.equals(ALPHA_NUMERIC)){
         negativeAccepted = negativeaccepted;
        acceptedChars += "-";
        }
      }

    @Override
   public void insertString(int offset, String  str, AttributeSet attr) throws BadLocationException {
     if (str == null) return;

     if (upperCase)
        str = str.toUpperCase();
     else if (acceptedChars.equals(LOWERCASE))
        str = str.toLowerCase();

     for (int i=0; i < str.length(); i++) {
       if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
         return;
    }

     if (acceptedChars.equals(FLOAT) ||
        (acceptedChars.equals(FLOAT + "-") && negativeAccepted)) {
        if (str.indexOf(".") != -1) {
           if (getText(0, getLength()).indexOf(".") != -1) {
              return;
           }
        }
     }

     if (negativeAccepted && str.indexOf("-") != -1) {
        if (str.indexOf("-") != 0 || offset != 0 ) {
           return;
           }
        }
     if(getLength() + str.length() > limit)
         return;

     if(onlyDayValues){
         String input = getText(0,getLength())+str;
         try{
             int value = Integer.parseInt(input);
             if(value<1 || value >31) return;
         }catch(NumberFormatException e){
             return;
         }
     }
     super.insertString(offset, str, attr);
   }

   public void setUpperCase(boolean value){
       this.upperCase = value;
   }

}